<?php
/**
 * 门店卡券发放核销统计
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019-03-28
 * Time: 下午 11:26
 */
namespace App;

use Model\AfterCouponMod;
use Model\GiveCouponMod;

class CouponStatistic extends Action
{
    private $GiveCoupon = null;   //发券记录
    private $AfterCoupon = null;  //核销记录

	public function __construct()
	{
		parent::__construct();
		$this->GiveCoupon = new GiveCouponMod();
        $this->AfterCoupon = new AfterCouponMod();
	}

    /**
     * 加载近期的卡券数据(今天、昨天、总数)
     * @param store_id 门店id
     */
    public function recentData()
    {
        $_today_timestamp_start = strtotime(date('Y-m-d'));
        $_today_timestamp_end = $_today_timestamp_start + 86400;
        //今天
        $_today_give = $this->GiveCoupon->total(array('store_id'=>$this->_gpc['store_id'],'time >='=>$_today_timestamp_start,'time <'=>$_today_timestamp_end));
        $_today_after = $this->AfterCoupon->total(array('store_id'=>$this->_gpc['store_id'],'time >='=>$_today_timestamp_start,'time <'=>$_today_timestamp_end));
        //昨天
        $_yesterday_give = $this->GiveCoupon->total(array('store_id'=>$this->_gpc['store_id'],'time >='=>($_today_timestamp_start-86400),'time <'=>($_today_timestamp_end-86400)));
        $_yesterday_after = $this->AfterCoupon->total(array('store_id'=>$this->_gpc['store_id'],'time >='=>($_today_timestamp_start-86400),'time <'=>($_today_timestamp_end-86400)));
        //总数
        $_total_give = $this->GiveCoupon->total(array('store_id'=>$this->_gpc['store_id']));
        $_total_after = $this->AfterCoupon->total(array('store_id'=>$this->_gpc['store_id']));
        $this->_result = array('status'=>'success','data'=>array(
            'today'=>array('give'=>$_today_give,'after'=>$_today_after),
            'yesterday'=>array('give'=>$_yesterday_give,'after'=>$_yesterday_after),
            'total'=>array('give'=>$_total_give,'after'=>$_total_after)
        ));
        $this->result($this->_result);
    }

    /**
     * 获取指定时间段的门端发券数量和核销数量
     * @param store_id 门店id
     * @param limit  时间段
     */
    public function startEndData()
    {
        $_give = array();
        $_after = array();
        for($i=$this->_gpc['limit'][0];$i<=$this->_gpc['limit'][1];$i+=86400){
            $_give[] = $this->GiveCoupon->total(array('store_id'=>$this->_gpc['store_id'],'time >='=>$i,'time <'=>($i+86400)));
            $_after[] = $this->AfterCoupon->total(array('store_id'=>$this->_gpc['store_id'],'time >='=>$i,'time <'=>($i+86400)));
        }
        $this->_result = array('status'=>'success','data'=>array(
            'give'=>$_give,
            'after'=>$_after
        ));
        $this->result($this->_result);
    }

    /**
     * 统计页面获取每天的发券、核销卡券、累计发券、累计核销的数量列表
     * @param store_id 门店id
     * @param limit 时间段  [最大的时间戳,天数]
     * @param create_time 门店的创建时间
     */
    public function getList()
    {
        $_data = array();
        if($this->_gpc['limit'][0] >= $this->_gpc['create_time']){
            $_start_time = $this->_gpc['limit'][0];
            $_end_time = $this->_gpc['limit'][0] - 86400*20;
            for($i=$_start_time;$i>$_end_time;$i-=86400){
                $_temp_array = array(
                    'time'=>date('m-d',$i),
                    'give'=>$this->GiveCoupon->total(array('store_id'=>$this->_gpc['store_id'],'time >='=>$i,'time <'=>($i+86400))),
                    'after'=>$this->AfterCoupon->total(array('store_id'=>$this->_gpc['store_id'],'time >='=>$i,'time <'=>($i+86400))),
                    'addup_give'=>$this->GiveCoupon->total(array('store_id'=>$this->_gpc['store_id'],'time <'=>($i+86400))),
                    'addup_after'=>$this->AfterCoupon->total(array('store_id'=>$this->_gpc['store_id'],'time <'=>($i+86400)))
                );
                $_data[] = $_temp_array;
            }
            $this->_result = array('status'=>'success','data'=>$_data);
        }else{
            $this->_result = array('status'=>'error');
        }
        $this->result($this->_result);
    }


}